package com.ydd.common.utils.excel.poi.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.ydd.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wumin
 * @version 1.0
 * @Description: EasyPoiUtil
 * @date 2021/5/10 10:12
 */
@Slf4j
public class EasyPoiUtil {

    /**
     * @param type Excel类型
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description: 创建Workbook
     * @author wumin
     * @date 2021-05-10 10:47
     */
    public static Workbook getWorkbook(ExcelType type) {
        return ExcelType.HSSF.equals(type) ? new HSSFWorkbook() : new XSSFWorkbook();
    }

    public static Workbook getWorkbook(ExcelType type, String templatePath) {
        ClassPathResource classPathResource = new ClassPathResource(templatePath);
        try (InputStream inputStream = classPathResource.getInputStream()) {
             return ExcelType.HSSF.equals(type) ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("获取模板失败");
        }
    }

    /**
     * @param url
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description:
     * @author wumin
     * @date 2021-05-10 11:49
     */
    public static Workbook getWorkbook(String url) {
        if (StringUtils.isBlank(url)) {
            throw new BusinessException("路径不能为空");
        }
        String type = StringUtils.substringAfterLast(url, ".");
        Resource resource = new ClassPathResource(url);
        try (InputStream inputStream = resource.getInputStream()) {
            return "xls".equals(type) ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("读取Excel文件失败！" + e.getMessage());
        }
    }

    /**
     * @param inputStream Excel流
     * @param pojoClass   导入实体
     * @return java.util.List<T>
     * @Description: 导入Excel
     * @author wumin
     * @date 2021-05-10 10:47
     */
    public static <T> List<T> importExcel(InputStream inputStream, Class<T> pojoClass) throws Exception {
        ImportParams params = new ImportParams();
        return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
    }

    /**
     * @param inputStream Excel流
     * @param pojoClass   导入实体
     * @param params      导入参数
     * @return java.util.List<T>
     * @Description: 导入Excel
     * @author wumin
     * @date 2021-05-10 10:46
     */
    public static <T> List<T> importExcel(InputStream inputStream, Class<T> pojoClass, ImportParams params) throws Exception {
        return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
    }

    /**
     * @param inputStream Excel流
     * @param pojoClass   导入实体
     * @param params      导入参数
     * @return cn.afterturn.easypoi.excel.entity.result.ExcelImportResult<T>
     * @Description: 导入Excel（支持返回校验结果）
     * @author wumin
     * @date 2021-05-10 13:36
     */
    public static <T> ExcelImportResult<T> importExcelMore(InputStream inputStream, Class<T> pojoClass, ImportParams params) throws Exception {
        return ExcelImportUtil.importExcelMore(inputStream, pojoClass, params);
    }

    /**
     * @param pojoClass 导出实体
     * @param dataSet   数据
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description: 导出Excel
     * @author wumin
     * @date 2021-05-10 10:45
     */
    public static Workbook exportExcel(Class<?> pojoClass, Collection<?> dataSet) {
        ExportParams params = new ExportParams();
        params.setType(ExcelType.XSSF);
        return ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
    }

    /**
     * @param params    导出参数
     * @param pojoClass 导出实体
     * @param dataSet   数据
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description: 导出Excel
     * @author wumin
     * @date 2021-05-10 10:44
     */
    public static Workbook exportExcel(ExportParams params, Class<?> pojoClass, Collection<?> dataSet) {
        return ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
    }

    /**
     * @param templateUrl 模板地址
     * @param map         数据
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description: 按模板导出
     * @author wumin
     * @date 2021-05-10 10:43
     */
    public static Workbook exportExcel(String templateUrl, Map<String, Object> map) {
        TemplateExportParams params = new TemplateExportParams();
        params.setTemplateUrl(templateUrl);
        return ExcelExportUtil.exportExcel(params, map);
    }

    /**
     * @param params 导出参数
     * @param map    数据
     * @return org.apache.poi.ss.usermodel.Workbook
     * @Description: 按模板导出
     * @author wumin
     * @date 2021-05-10 10:44
     */
    public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) {
        return ExcelExportUtil.exportExcel(params, map);
    }

    /**
     * @param templateUrl 模板地址
     * @param map         数据
     * @return org.apache.poi.xwpf.usermodel.XWPFDocument
     * @Description: 导出Excel
     * @author wumin
     * @date 2021-05-10 10:53
     */
    public static XWPFDocument exportWord07(String templateUrl, Map<String, Object> map) throws Exception {
        return WordExportUtil.exportWord07(templateUrl, map);
    }

}
